package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.Merchants;
import com.li.shop.domain.MerchantsExample;
import com.li.shop.mapper.MerchantsMapper;
import com.li.shop.service.MerchantsService;
import com.li.shop.utils.SearchMerchantsRequest;
import com.li.shop.utils.SearchMerchantsResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantsServiceImpl implements MerchantsService {

    @Autowired
    private MerchantsMapper merchantsMapper;

    @Override
    public void add(Merchants merchants) {
        merchantsMapper.insert(merchants);
    }

    @Override
    public void delete(String email) {
        merchantsMapper.deleteByPrimaryKey(email);
    }

    @Override
    public void update(Merchants merchants) {
        merchantsMapper.updateByPrimaryKeySelective(merchants);
    }

    @Override
    public Merchants login(String email, String password) {
        MerchantsExample example=new MerchantsExample();
        example.createCriteria().andEmailEqualTo(email).andPasswordEqualTo(password);
        List<Merchants> merchantss=merchantsMapper.selectByExample(example);
        if(merchantss.isEmpty())
        {
            return null;
        }
        return merchantss.get(0);
    }

    @Override
    public Merchants get(String email) {
        return merchantsMapper.selectByPrimaryKey(email);
    }

    @Override
    public List<Merchants> list() {
        MerchantsExample example = new MerchantsExample();
        example.setOrderByClause("id desc");
        List<Merchants> merchantss = merchantsMapper.selectByExample(example);
        return merchantss;
    }

    @Override
    public Merchants get(String email, String password) {
        MerchantsExample example = new MerchantsExample();
        example.createCriteria().andEmailEqualTo(email).andPasswordEqualTo(password);
        List<Merchants> merchantss = merchantsMapper.selectByExample(example);
        if(merchantss.isEmpty())
            return null;
        return merchantss.get(0);
    }

    @Override
    public boolean exist(String email) {
        MerchantsExample example = new MerchantsExample();
        example.createCriteria().andEmailEqualTo(email);
        List<Merchants> merchantss = merchantsMapper.selectByExample(example);
        if(merchantss.isEmpty()){
            return false;
        }
        return true;
    }



    //王志文
    @Override
    //public List<User> listUser(Integer pageSize,Integer pageNum) {
    public SearchMerchantsResponse listMerchants(Integer pageSize, Integer pageNum, String keyword) {
        //PageHelper.startPage(1,10);
        PageHelper.startPage(pageNum,pageSize);
        MerchantsExample example =new MerchantsExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andNameLike("%"+keyword+"%");
        }
        List<Merchants> merchantss= merchantsMapper.selectByExample(example);
        return  new SearchMerchantsResponse(((Page)merchantss).getTotal(),merchantss);
        //return UserMapper.selectByExample(new UserExample());//分页后返回的page对象，page中有一个list对象
    }


    @Override
    public int deleteMerchantsByPrimaryKey(String id) {
        return merchantsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Merchants findMerchantsByPrimaryKey(String id) {
        return merchantsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Merchants> searchMerchantss(SearchMerchantsRequest request) {
        MerchantsExample ex = new MerchantsExample();
//        ex.createCriteria().andUserIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return merchantsMapper.selectByExample(ex);
    }

    @Override
    public int saveMerchants( Merchants merchants) {
        System.out.println(merchants.getmId()+"merchantsid");
        if(StringUtils.isNotBlank( merchants.getmId()) ){
            //if( user.getuId()!=null ){
            //把domain类中的uId类型转化为String
            return merchantsMapper.updateByPrimaryKey(merchants);
        } else {
            return merchantsMapper.insert(merchants);
        }
    }

}
